# libs ----
library(sf)
library(geomander)
library(tidyverse)
library(redist)
library(ppmf)
library(grid)
library(patchwork)
library(wacolors)


# helper ----
source('../00_custom_functions.R')

# data ----
state <- "LA"
ppmf19 <- read_ppmf(state, "../../data-raw/ppmf_19.csv")
ppmf12 <- read_ppmf(state, "../../data-raw/ppmf_12.csv")
ppmf4 <- read_ppmf(state, "../../data-raw/ppmf_04.csv")

ppmf19 <- ppmf19 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf19) <- paste("v19", colnames(ppmf19), sep = "_")
ppmf19 <- ppmf19 %>% rename(GEOID = v19_GEOID)

ppmf12 <- ppmf12 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf12) <- paste("v12", colnames(ppmf12), sep = "_")
ppmf12 <- ppmf12 %>% rename(GEOID = v12_GEOID)

ppmf4 <- ppmf4 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf4) <- paste("v4", colnames(ppmf4), sep = "_")
ppmf4 <- ppmf4 %>% rename(GEOID = v4_GEOID)

# comparison ----
census <- create_block_table(state = 'LA')

# all joined ----
mega <- census %>%
  left_join(ppmf12, by = 'GEOID') %>%
  left_join(ppmf4, by = 'GEOID') %>%
  left_join(ppmf19, by = "GEOID")

# and remove duplicates
mega <- mega %>% select(-contains('.'))
# and set missing block pop/vap to 0
mega[is.na(mega)] <- 0
# add block_group back (dropped by contains('.'))
mega <- mega %>% breakdown_geoid()

# from VEST 2018
prec <- st_read('../../data/LA/la_2018/la_2018.shp')
prec <- prec %>% st_transform(st_crs(mega))

# Get Census Shapes :
sld_low <- tigris::state_legislative_districts('LA', 'lower')
sld_up <- tigris::state_legislative_districts('LA', 'upper')
cd <- tigris::congressional_districts('LA')
vtd <- tigris::voting_districts('LA')

sf::sf_use_s2(FALSE)
# Align ppmf & precincts:
block_prec_match <- geo_match(from = mega, to = prec, method = 'centroid')
block_vtd_match <- geo_match(from = mega, to = vtd, method = 'centroid')
vtd_low_match <- geo_match(from = vtd, to = sld_low, method = 'area')
vtd_up_match <- geo_match(from = vtd, to = sld_up, method = 'area')
vtd_cd_match <- geo_match(from = vtd, to = cd, method = 'area')

vtd <- vtd %>% mutate(
  sld_low = sld_low$SLDLST[vtd_low_match],
  sld_up = sld_up$SLDUST[vtd_up_match],
  cd = cd$CD116FP[vtd_cd_match]
)

mega$vtd <- block_vtd_match
ppmf_at_vtd <- ppmf_block2prec(mega, vtd)

# join them together
la <- vtd %>%
  mutate(vtd = row_number()) %>%
  left_join(ppmf_at_vtd, by = 'vtd')

la$cd <- as.numeric(la$cd)
la$sld_low <- as.numeric(la$sld_low)
la$sld_up <- as.numeric(la$sld_up)
la <- la %>% mutate(row_id = row_number())

# project down elec ----
mega$sos_vest_REP <- estimate_down(wts = mega$vap, value = prec$R18SOSRARD, group = block_prec_match)
mega$sos_vest_DEM <- estimate_down(wts = mega$vap, value = prec$R18SOSDCOL, group = block_prec_match)
mega$sos_vest_REP_gen <- estimate_down(wts = mega$vap,
                                       value = prec$R18SOSRARD + prec$G18SOSRCLO + prec$G18SOSRCRO +
                                         prec$G18SOSREDM + prec$G18SOSRKEN + prec$G18SOSRSTO,
                                       group = block_prec_match)
mega$sos_vest_DEM_gen <- estimate_down(wts = mega$vap, value = prec$G18SOSDCOL + prec$G18SOSDFRE,
                                       group = block_prec_match)
mega$sos_vest_OTH_gen <- estimate_down(wts = mega$vap, value = prec$G18SOSNMOR,
                                       group = block_prec_match)


# agg up elec ----
la$sos_vest_REP <- estimate_up(value = mega$sos_vest_REP, group = block_vtd_match)
la$sos_vest_DEM <- estimate_up(value = mega$sos_vest_DEM, group = block_vtd_match)
la$sos_vest_OTH <- 0
la$sos_vest_REP_gen <- estimate_up(value = mega$sos_vest_REP_gen, group = block_vtd_match)
la$sos_vest_DEM_gen <- estimate_up(value = mega$sos_vest_DEM_gen, group = block_vtd_match)
la$sos_vest_OTH_gen <- estimate_up(value = mega$sos_vest_OTH_gen, group = block_vtd_match)

saveRDS(la, '../../data/LA/la.Rds', compress = 'xz')
